import requests
import json
from datetime import datetime

def get_capital_rate_hyperliquid(token_array):
  # for i in range(0,len(token_array)):
  #   parts = token_array[i].rsplit('-', 1)
  #   token_array[i] = parts[0]  # "BTC-USDT-SWAP"

  url = "https://api.hyperliquid.xyz/info"

  payload = json.dumps({
    "type": "metaAndAssetCtxs"
  })
  headers = {
    'Content-Type': 'application/json'
  }

  response = requests.request("POST", url, headers=headers, data=payload)
  json_data=response.json()
  #数组
  universe=json_data[0]["universe"]
  #数组
  all_funding_rate=json_data[1]
  timestamp = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
  result=[]
  for i in range(0,len(universe)):
    for j in range(0,len(token_array)):
      if(universe[i]["name"]in token_array[j]):
        result.append({"time":timestamp,"trading_pair":universe[i]["name"],"platform":"hyperliquid","funding":float(all_funding_rate[i]["funding"])*100})
  return result

